﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PersonalHealth.Data.Model;
using PersonalHealth.Data.Service;
using PersonalHealth.Data.IService;

namespace PersonHealth.View
{
    /// <summary>
    /// ECGList.xaml 的交互逻辑
    /// </summary>
    public partial class ECGList : Page
    {
         int pageIndex = 1;
        int pageCount = 5;
        int year = DateTime.Now.Year;
        int month = DateTime.Now.Month;

        public ECGList()
        {
            InitializeComponent();

            pageIndex = 1;
            bindListData();
        }


        private void Previous_Click(object sender, MouseButtonEventArgs e)
        {
            if (pageIndex == 1)
                return;
            pageIndex--;
            bindListData();
        }

        private void Next_Click(object sender, MouseButtonEventArgs e)
        {

            if (this.DataContext != null && pageIndex == (this.DataContext as ECGModel).Pages)
                return;
            pageIndex++;
            bindListData();
        }

        private void bindListData()
        {
            MeasureUser user = AppData.User;
            IMeasure_ECGService ser = new Measure_ECGService();

            int pages = 0;
            int records = 0;
            Func<Measure_ECG, DateTime>[] orderby = new Func<Measure_ECG, DateTime>[] { new Func<Measure_ECG, DateTime>(c => c.MeasureTime) };
            bool[] isdesc = new bool[] { true };
            IList<Measure_ECG> objs = ser.Find<DateTime>(c => c.UserId == user.Id, orderby, isdesc, pageIndex, pageCount, out pages, out records);
            if (objs != null)
            {
                gv.ItemsSource = objs;

                ECGModel model = new ECGModel();
                model.PageIndex = pageIndex;
                model.PageCount = pageCount;
                model.Pages = pages;
                model.Records = records;
                this.DataContext = model;
            }
        }

        private void gv_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            var obj = e.AddedItems[0] as Measure_ECG;

            NavigationService.Content = new ECGForm(obj);
        }
    }

    public class ECGModel
    {
        public int Records { get; set; }
        public int PageCount { get; set; }

        public int PageIndex { get; set; }

        public int Pages { get; set; }
    }
}
